﻿// *----------------------------------------------------------------
// Copyright (C) 2017 通通优品
// 版权所有。
// 
// 类名：TongTongMallBootstrapper.cs
// 功能描述：TongTongMall
// 
// Create User：jym 2017/02/09
// 
// Edit User：UserName,EditTime
// Describe：Describe
// ----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TongTongMall.Configuration.Startup;
using TongTongMall.Dependecy;
using TongTongMall.Dependecy.CoreInstaller;
using TongTongMall.Modules;

namespace TongTongMall
{
    public class TongTongMallBootstrapper
    {
        protected bool IsDisposable;

        private IModuleManager _moduleManager;

        public IIocManager IocManager { get; }

        public TongTongMallBootstrapper() : this(Dependecy.IocManager.Instance)
        {
        }

        public TongTongMallBootstrapper(IIocManager iocManager)
        {
            IocManager = iocManager;
        }

        public virtual void Initialize()
        {
            //IIocManager,IModuleManager,UnitOfWorkDefaultOptions
            IocManager.IocContainer.Install(new CoreInstaller());

            IocManager.Resolve<IStartupConfiguration>().Initialize();

            _moduleManager = IocManager.IocContainer.Resolve<IModuleManager>();
            _moduleManager.InitializeModules();
        }

        public void Dispose()
        {
            if (IsDisposable) return;
            IsDisposable = true;
            if (_moduleManager != null)
            {
                _moduleManager.ShutdownModules();
            }
        }
    }
}
